from flask import Blueprint, request, abort
from flask_restful import Resource, Api, marshal_with, fields
from models import Community_PostModel, PostComment, PostLike
from exts import db

# 创建蓝图
post_query_bp = Blueprint('post_query', __name__)
api = Api(post_query_bp)

# 字段定义
comment_fields = {
    'id': fields.Integer,
    'content': fields.String,
    'user_id': fields.Integer,
    'create_time': fields.DateTime
}

post_list_fields = {
    'id': fields.Integer,
    'title': fields.String,
    'content': fields.String,
    'images': fields.Raw,  
    'user_id': fields.Integer,
    'create_time': fields.DateTime,
    'like_count': fields.Integer,
    'comment_count': fields.Integer,
    'post_type': fields.String,
    'partner_type': fields.String,
    'community_type': fields.String
}

post_detail_fields = {
    'id': fields.Integer,
    'title': fields.String,
    'content': fields.String,
    'images': fields.List(fields.String),
    'user_id': fields.Integer,
    'create_time': fields.DateTime,
    'like_count': fields.Integer,
    'comment_count': fields.Integer,
    'post_type': fields.String,
    'partner_type': fields.String,
    'community_type': fields.String,
    'comments': fields.List(fields.Nested(comment_fields)) if comment_fields else fields.List(fields.String),
    'is_liked': fields.Boolean
}

class PostList(Resource):
    @marshal_with(post_list_fields)
    def post(self):
        try:
            data = request.get_json()
            page = data.get('page', 1)
            per_page = data.get('per_page', 10)
            post_type = data.get('post_type')
            partner_type = data.get('partner_type')
            community_type = data.get('community_type')

            query = Community_PostModel.query

            if post_type:
                query = query.filter_by(post_type=post_type)
                # 如果是partner类型，且指定了partner_type，进行二次筛选
                if post_type == 'partner' and partner_type:
                    query = query.filter_by(partner_type=partner_type)
                # 如果是community类型，且指定了community_type，进行二次筛选
                elif post_type == 'community' and community_type:
                    query = query.filter_by(community_type=community_type)

            posts = query.order_by(Community_PostModel.create_time.desc())\
                .paginate(page=page, per_page=per_page, error_out=False)

            return posts.items
        except Exception as e:
            return {'error': str(e)}, 400

class PostDetail(Resource):
    @marshal_with(post_detail_fields)
    def get(self, post_id):
        try:
            post = Community_PostModel.query.get(post_id)
            if not post:
                abort(404, message='帖子不存在')
                
            comments = PostComment.query.filter_by(post_id=post_id)\
                .order_by(PostComment.create_time.desc()).all()
                
            result = {
                'id': post.id,
                'title': post.title,
                'content': post.content,
                'images': post.images,
                'user_id': post.user_id,
                'create_time': post.create_time,
                'like_count': post.like_count,
                'comment_count': post.comment_count,
                'post_type': post.post_type,
                'partner_type': post.partner_type,
                'community_type': post.community_type,
                'comments': comments,
                'is_liked': False
            }
            
            return result
            
        except Exception as e:
            return {'error': str(e)}, 400

# 注册路由
api.add_resource(PostList, '/post/query')
api.add_resource(PostDetail, '/post/detail/<int:post_id>') 